package com.team.olympic.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.team.olympic.service.UserService;
import com.team.olympic.dto.UserVoteDto;
import com.team.olympic.pojo.Response;
import com.team.olympic.pojo.User;

@CrossOrigin
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/users")
    public Response<?> findAllUsers() {
        return Response.success(userService.findAllUsers());
    }

    /**
     * 根据id查找用户
     * @param userId 用户id
     * 写法如下
     * http://localhost:8080/user?userId=506746170312364032
     * @return Response(包含用户信息) {@link com.team.olympic.pojo.Response}
     */
    @GetMapping("/user")
    public Response<?> findUserById(@RequestParam Long userId) {
        return Response.success(userService.findUserById(userId));
    }

    /**
     * 用户登录
     * @param user
     * 传入json如下
     * {
     *     "phone": "13813386277",
     *     "password": "123456"
     * }
     * @return Response(包含登录的用户信息) {@link com.team.olympic.pojo.Response}
     */
    @PostMapping("/login")
    public Response<?> login(@RequestBody User user) {
        User loginUser = userService.login(user);
        if (loginUser != null) {
            return Response.success("登录成功", loginUser);
        }
        return Response.error("用户名或密码错误或被封禁");
    }

    /**
     * 用户注册
     * @param user
     * 传入json如下
     * {
     *     "phone": "13813386277",
     *     "password": "123456"
     * }
     * @return Response(包含注册的用户信息) {@link com.team.olympic.pojo.Response}
     */
    @PostMapping("/register")
    public Response<?> register(@RequestBody User user) {
        User registerUser = userService.register(user);
        if (registerUser != null) {
            return Response.success("注册成功", registerUser);
        }
        return Response.error("该手机号已被注册或参数错误");
    }

    /**
     * 用户投票
     * @param userVoteDto 用户投票信息
     * 传入json如下
     * {
     *     "userId": "506746170312364032",
     *     "matchId": "1",
     *     "win": "true"
     * }
     * @return Response {@link com.team.olympic.pojo.Response}
     */
    @PostMapping("/vote")
    public Response<?> userVote(@RequestBody UserVoteDto userVoteDto) {
        if (userService.userVote(userVoteDto)) {
            return Response.success("投票成功", userService.findUserById(userVoteDto.getUserId()));
        }
        return Response.error("投票失败");
    }

    /**
     * 根据id删除用户
     * @param userId 用户id
     * 写法如下
     * http://localhost:8080/deleteUserById?userId=506746170312364032
     * @return Response {@link com.team.olympic.pojo.Response}
     */
    @PostMapping("/deleteUserById")
    public Response<?> deleteUserById(@RequestParam Long userId) {
        userService.deleteUserById(userId);
        return Response.success("删除成功");
    }

    /**
     * 根据id减少用户分数
     * @param userId 用户id
     * @param score 减少的分数
     * 写法如下
     * http://localhost:8080/declineScore?userId=506798796617945088&score=5
     * @return Response(包含最新用户信息) {@link com.team.olympic.pojo.Response}
     */
    @PostMapping("/declineScore")
    public Response<?> declineUserScoreById(@RequestParam Long userId, @RequestParam Long score) {
        userService.declineUserScoreById(userId, score);
        return Response.success(findUserById(userId));
    }

    /**
     * 用户兑换物品
     * @param userId 用户id
     * @param itemId 物品id
     * 写法如下
     * http://localhost:8080/buyItem?userId=506798796617945088&itemId=1
     * @return Response(包含最新用户信息) {@link com.team.olympic.pojo.Response}
     */
    @PostMapping("/buyItem")
    public Response<?> buyItem(@RequestParam Long userId, @RequestParam Long itemId) {
        if (userService.buyItem(userId, itemId)) {
            return Response.success(findUserById(userId));
        }
        return Response.error("兑换失败");
    }

    /**
     * 根据id封禁用户
     * @param userId 用户id
     * 写法如下
     * http://localhost:8080/blockUser?userId=506798796617945088
     * @return Response(包含最新用户信息) {@link com.team.olympic.pojo.Response}
     */
    @PostMapping("/blockUser")
    public Response<?> blockUser(@RequestParam Long userId) {
        userService.blockUser(userId);
        return Response.success("封禁成功", findUserById(userId));
    }

    /**
     * 根据id解封用户
     * @param userId 用户id
     * 写法如下
     * http://localhost:8080/freeUser?userId=506798796617945088
     * @return Response(包含最新用户信息) {@link com.team.olympic.pojo.Response}
     */
    @PostMapping("/freeUser")
    public Response<?> freeUser(@RequestParam Long userId) {
        userService.freeUser(userId);
        return Response.success("解封成功", findUserById(userId));
    }

    /**
     * 更新用户信息
     * @param user 用户信息
     * 传入json如下
     * 传入json如下
     * {
     *     "id": "506798796617945088",
     *     "name": "萧云灼",
     *     "address": "北京市海淀区"
     * }
     * @return Response(包含最新用户信息) {@link com.team.olympic.pojo.Response}
     */
    @PostMapping("/updateUser")
    public Response<?> updateUser(@RequestBody User user) {
        if (userService.updateUser(user)) {
            return Response.success("更新成功", findUserById(user.getId()));
        }
        return Response.error();
    }

    @GetMapping("/myItem")
    public Response<?> findMyItem(@RequestParam Long userId) {
        return Response.success(userService.findMyItems(userId));
    }
}
